package com.jinzhi.jzweb.controller;


import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.system.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.bo.LogBO;
import com.jinzhi.common.config.Constant;
import com.jinzhi.common.service.LogService;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.*;
import com.jinzhi.jzweb.pojo.bo.ExpenseTotalBO;
import com.jinzhi.jzweb.pojo.bo.ProvinceCostStandardBO;
import com.jinzhi.jzweb.pojo.bo.PublicAndPrivateExpesetotalBo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.domain.RoleDO;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.domain.UserRoleDO;
import com.jinzhi.sys.service.RoleService;
import com.jinzhi.sys.service.UserRoleService;
import com.mysql.jdbc.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2022-03-21 14:07:25 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/publicRefund")
public class PublicRefundController extends BaseController {

	//获取项目的根路径
	@Value("${easypoi.path}")
	private String classPath;

	@Autowired
	private PublicRefundService publicRefundService;

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private ExaminationCostService examinationCostService;

	@Autowired
	private RoleService roleService;


	@Autowired
	private OrganizationNewsService organizationNewsService; //学校档案表


	@Autowired
	private ConsultationsService consultationsService; //资讯表


	@Autowired
	private LogService logService;

	@Autowired
	private ArchivesService archivesService;

	@GetMapping()
	@RequiresPermissions("jzweb:publicRefund:publicRefund")
	String PublicRefund(Integer year,Model model){
		model.addAttribute("year",year);
	    return "jzweb/publicRefund/publicRefund";
	}



	/**
	 * 私有处理方法
	 */
	private String RootPath(String u_path) {
		String rootPath = "";
		//windows下
		if ("\\".equals(File.separator)) {
			//System.out.println(classPath);
			rootPath = classPath + u_path;
			rootPath = rootPath.replaceAll("/", "\\\\");
			if (rootPath.substring(0, 1).equals("\\")) {
				rootPath = rootPath.substring(1);
			}
		}
		//linux下
		if ("/".equals(File.separator)) {
			//System.out.println(classPath);
			rootPath = classPath + u_path;
			rootPath = rootPath.replaceAll("\\\\", "/");
		}
		return rootPath;
	}




	//授权方法
	private void accredit(Map map){
		//获取用户权限
		Integer is = userRoleService.userRolenum(this.getUserId());
		List<RoleDO>  listRole=roleService.findAll();
		Long chOO1=0l;
		Long chOO2=0l;
		if(is==6){
			List lists=new ArrayList();
			List list=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
			for(UserRoleDO users:userRoleDOS){
				list.add(users.getUserId());
			}
			map.put("list1",list);
		}else if(is==7){
			List lists=new ArrayList();
			List list=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			for(UserRoleDO users:userRoleDOS){
				list.add(users.getUserId());
			}
			map.put("list1",list);
		}else if(is==8){
			map.put("list2",this.getUserId().toString());
		}else if(is==9){
			map.put("list2",this.getUserId().toString());
		}else if(is==18){
			map.put("list2",this.getUserId().toString());
		}else if(is==19){
			map.put("list2",this.getUserId().toString());
		}
	}

//	//费用默认值
//	private PublicAndPrivateExpesetotalBo bigDecimalDef(PublicAndPrivateExpesetotalBo publicAndPrivateExpesetotalBo){
//		if (null==publicAndPrivateExpesetotalBo){
//			publicAndPrivateExpesetotalBo=new PublicAndPrivateExpesetotalBo();
//			publicAndPrivateExpesetotalBo.setTypePublic(new BigDecimal(0.0));
//			publicAndPrivateExpesetotalBo.setTypePrivate(new BigDecimal(0.0));
//			return publicAndPrivateExpesetotalBo;
//		}
//		if (null==publicAndPrivateExpesetotalBo.getTypePublic()){
//			publicAndPrivateExpesetotalBo.setTypePublic(new BigDecimal(0.0));
//		}
//		if (null==publicAndPrivateExpesetotalBo.getTypePrivate()){
//			publicAndPrivateExpesetotalBo.setTypePrivate(new BigDecimal(0.0));
//		}
//		return publicAndPrivateExpesetotalBo;
//	}


	//费用信息格式化
//	private String  msgFormat(ExpenseTotalBO expenseTotalBO){
//		Date date=new Date();
//		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
//
//		String msg="导出时间："+sdf.format(date)+" " +
//				" （劳务费合计："+expenseTotalBO.getLaborCostTotal()+
//				"） （餐费费合计："+expenseTotalBO.getMealsTotal()+"） （市内交通费合计："+expenseTotalBO.getTrafficExpenseTotal()+
//				"） （住宿费合计："+expenseTotalBO.getHotelExpenseTotal()+"） （邮费合计："+expenseTotalBO.getPostageTotal()+
//				"） （大交通费合计："+expenseTotalBO.getTrafficExpenseMaxTotal()+"） （其它费用："+expenseTotalBO.getOtherCostTotal()+
//				"）" +
//				"	总合计："+expenseTotalBO.getSumExpenseTotal();
//		return msg;
//	}






	//获取docx文件位置
	private File templateFile(){


		File templateFile= new File(RootPath("/easypoi/publicRefnd.docx"));

		System.out.printf("++++++++++++++++++++++++++++++++++++++++++++++"+templateFile.getPath());

		return templateFile;
	}



	/**
	 * 列表查询
	 * @param publicRefundVO
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:publicRefund:publicRefund")
	public Result<Map<String,Object>> list(PublicRefundVO publicRefundVO, @RequestParam(required = false,defaultValue = "1")int pageNumber, @RequestParam(required = false,defaultValue = "10")int pageSize){

		Map map=new HashMap();
		//进行授权
		accredit(map);
		//查询条件赋值
		map.put("refundType",publicRefundVO.getRefundType());
		map.put("pageNumber",pageNumber);
		map.put("pageSize",pageSize);
		map.put("refundRrNot",publicRefundVO.getRefundRrNot());
		map.put("batchId",publicRefundVO.getBatchId());
		map.put("batchId2",publicRefundVO.getBatchId2());
		map.put("year",publicRefundVO.getYear());
		map.put("province",publicRefundVO.getProvince());
		map.put("evaluationSchool",publicRefundVO.getSchoolName());
		map.put("regionUserName",publicRefundVO.getRegionUserName());
		map.put("expertName",publicRefundVO.getExpertName());
		map.put("examStartDate", StringUtil.notEmpty(publicRefundVO.getExamStartDate())?publicRefundVO.getExamStartDate()+" 00:00:00":publicRefundVO.getExamStartDate());
		map.put("examEndDate",StringUtil.notEmpty(publicRefundVO.getExamEndDate())?publicRefundVO.getExamEndDate()+" 00:00:00":publicRefundVO.getExamEndDate());



		List<PublicRefundDO> list1=publicRefundService.listPublicRefund(map);
		List list = new ArrayList();


		//定义总价对象集合
		List<ExpenseTotalBO> expenseTotalBOS =new ArrayList<>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < list1.size(); i++) {
			ExpenseTotalBO expenseTotalBO =new ExpenseTotalBO();

			PublicRefundDO obj =new PublicRefundDO();
			obj=list1.get(i);
			obj.setEvaluationTimes(list1.get(i).getEvaluationTime()!=null?sdf.format(list1.get(i).getEvaluationTime()):null);

			list.add(obj);
			expenseTotalBOS.add(expenseTotalBO);
		}
		//金额计算
		PublicAndPrivateExpesetotalBo publicAndPrivateExpesetotalBo =publicRefundService.moneyTotal(map);


		//分页数据
		Page<PublicRefundDO> page =new Page<>();
		page.setRecords(list);
		page.setCurrent(pageNumber);
		Integer total = publicRefundService.listPublicRefundCount(map);
		page.setTotal(total);
		page.setSize(pageSize);

		//封装返回值
		Map<String,Object> retMap =new HashMap();
		retMap.put("page",page);
		retMap.put("moneyTotal",publicAndPrivateExpesetotalBo);
		return Result.ok(retMap);
	}



	/**
	 * 下载返费信息录入导入模板
	 *
	 * @param response
	 */

	@GetMapping("/downloadImportTemplate")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	public void downloadImportTemplate(HttpServletResponse response,String examFeeIf) throws Exception {


		String path = RootPath("/easypoi/publicRefundExcel.xls");
		ExaminationCostDO examinationCostDO=examinationCostService.selectById(examFeeIf);

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

		TemplateExportParams params =new TemplateExportParams(path);
		String title = "关于1+X"+examinationCostDO.getProfile()+"职业技能等级证书考核站点返费的凭据";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title",title);
		map.put("orgName",examinationCostDO.getOrgName());
		map.put("profile",examinationCostDO.getProfile());
		map.put("examinationDate",sdf.format(examinationCostDO.getExaminationDate()));
		map.put("stuNum",examinationCostDO.getStuNum());
		String rank = examinationCostDO.getProfile().substring(examinationCostDO.getProfile().indexOf("-")+1);
		map.put("rank",rank);
		Map<String, String> proviceMap = costStandard(examinationCostDO.getProvince(), examinationCostDO.getProfile());
		//证书收费标准
//		String costStandard = proviceMap.get("costStandard");
		map.put("unitMoney", examinationCostDO.getPriceStandard());
		map.put("returnMoney",examinationCostDO.getReturnMoney());
		map.put("returnMoneySchool",examinationCostDO.getReturnMoneySchool());
		map.put("returnMoneyPersonal",examinationCostDO.getReturnMoneyPersonal());//对私返款金额
		map.put("type","学校");
		map.put("returnType","对公");
		map.put("remark","系统数据");


		Workbook workbook = ExcelExportUtil.exportExcel(params, map);
		if (workbook == null) {
			//读取模板失败
		}
		String fileName = "关于1+X"+examinationCostDO.getProfile()+"职业技能等级证书考核站点返费的凭据" + ".xls";


		// 重置响应对象
		response.reset();
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setContentType("textml;charset=utf-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 写出数据输出流到页面
		try {
			OutputStream output = response.getOutputStream();
			BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
			workbook.write(bufferedOutPut);
			bufferedOutPut.flush();
			bufferedOutPut.close();
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private Map<String,String> costStandard(String province,String grade){

	Map<String, ProvinceCostStandardBO> map =new HashMap<>();
	ProvinceCostStandardBO provinceCostStandardBO =new ProvinceCostStandardBO();
	//默认标准
		provinceCostStandardBO.setYrzhCostchu("368");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("368");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("368");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("80");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
		if ("山东省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("358");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("358");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("358");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("85");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("广西省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("368");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("368");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("368");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("80");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("411");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("411");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("411");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("100");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("吉林省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("415");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("415");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("415");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("100");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("黑龙江省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("415");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("415");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("415");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("100");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("95");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("宁夏回族自治区".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("377");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("377");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("377");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("80");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("安徽省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("368");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("368");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("368");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("80");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("95");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}

		if ("重庆市".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("400");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("415");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("430");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("100");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("102");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}

		if ("贵州省".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("368");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("368");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("368");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("85");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}

		if ("北京市".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("368");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("368");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("368");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("95");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("395");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("395");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("395");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("90");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}
		if ("内蒙古自治区".equals(province)){
		provinceCostStandardBO.setYrzhCostchu("315");//幼儿照护初级收费标准
		provinceCostStandardBO.setYrzhCostzhong("315");//幼儿照护中级收费标准
		provinceCostStandardBO.setYrzhCostgao("315");//幼儿照护高级收费标准
		provinceCostStandardBO.setYrzhreturnCost("55");//幼儿照护初中高返费标准
		provinceCostStandardBO.setChhfCostchu("311");//产后恢复初级收费标准
		provinceCostStandardBO.setChhfCostzhong("311");//产后恢复中级收费标准
		provinceCostStandardBO.setChhfCostgao("311");//产后恢复高级收费标准
		provinceCostStandardBO.setChhfreturnCost("51");//产后恢复初中高返费标准
		map.put(province,provinceCostStandardBO); //省份 + 费用标准
	}

		return setProvinceCostStandardBO(province,grade,map);
}



	//省份价格标准初始化
	private Map<String,String> setProvinceCostStandardBO(String province,String grade,Map<String,ProvinceCostStandardBO> map){
		Map<String,String> rstMap =new HashMap<>();
		ProvinceCostStandardBO provinceCostStandardBO = map.get(province);
		if ("幼儿照护-初级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getYrzhCostchu());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getYrzhreturnCost());
		}
		if ("幼儿照护-中级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getYrzhCostzhong());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getYrzhreturnCost());
		}
		if ("幼儿照护-高级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getYrzhCostgao());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getYrzhreturnCost());
		}
		if ("产后恢复-初级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getChhfCostchu());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getChhfreturnCost());
		}
		if ("产后恢复-中级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getChhfCostzhong());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getChhfreturnCost());
		}
		if ("产后恢复-高级".equals(grade)){
			rstMap.put("costStandard",provinceCostStandardBO.getChhfCostgao());
			rstMap.put("returnCostStandard",provinceCostStandardBO.getChhfreturnCost());
		}
		return rstMap;
	}





//	/**
//	 * 下载返费信息录入导入模板
//	 *
//	 * @param response
//	 */
//
//	@GetMapping("/downloadImportTemplate")
//	@RequiresPermissions("jzweb:publicRefund:import")
//	@ResponseBody
//	public void downloadImportTemplate(HttpServletResponse response,String examFeeIf) {
//		ExaminationCostDO examinationCostDO=examinationCostService.selectById(examFeeIf);
//
//		EntityWrapper<ArchivesDO> wrapper =new EntityWrapper<>();
//		wrapper.eq("school_name",examinationCostDO.getOrgName());
//		List<ArchivesDO> archivesDOS = archivesService.selectList(wrapper);
//
//		List<PublicRefundImportVO> list = new LinkedList<>();
//		PublicRefundImportVO publicRefundImportVO =new PublicRefundImportVO();
//		publicRefundImportVO.setExpertName(examinationCostDO.getOrgName());
//		publicRefundImportVO.setExpertType("学校");
//		publicRefundImportVO.setRefundType("对公");
//		publicRefundImportVO.setCost(new BigDecimal(examinationCostDO.getReturnMoneySchool() == null?0:examinationCostDO.getReturnMoneySchool()));
//		publicRefundImportVO.setRemarks("系统导出");
//
//		if (archivesDOS.size()>0){
//			if ((archivesDOS.get(0).getBankAccount()!=null&&!"".equals(archivesDOS.get(0).getBankAccount())) ||
//					(archivesDOS.get(0).getBankOfDeposit()!=null&&!"".equals(archivesDOS.get(0).getBankOfDeposit()))
//			){
//				publicRefundImportVO.setBankAccount(archivesDOS.get(0).getBankAccount());
//				publicRefundImportVO.setBankOfDeposit(archivesDOS.get(0).getBankOfDeposit());
//			}
//		}
//		list.add(publicRefundImportVO);
//		//导出操作
//		EasyPoiUtil.exportExcel(list, "2022考试费返费信息导入模板", "2022考试费返费信息导入模板", PublicRefundImportVO.class, "2022考试费返费信息详情导入模板", response);
//	}



	/**
	 * 考试费用详情表导入
	 */
	@Log("考试费用详情表导入")
	@PostMapping("/import")
	@ResponseBody
	@Transactional
	@RequiresPermissions("jzweb:publicRefund:import")
	public Result<?> importExcel(@RequestParam("file") MultipartFile file, Integer year, String examFeeIf) {
		//---------------------------------检测导入信息及条件-----------------------------------------
		//第一步--判断文件格式
		if (!"application/vnd.ms-excel".equals(file.getContentType())) {
			return Result.fail("文件格式错误，请选择.xls文件");
		}
		if(StringUtils.isNullOrEmpty(examFeeIf)){
			return Result.fail("请选择考试费学校");
		}

		ExaminationCostDO examinationCostDO=examinationCostService.selectById(examFeeIf);
		if(examinationCostDO!=null){
			if(!examinationCostDO.getYear().toString().equals(year.toString())){
				return Result.fail("所选考试批次非:"+year+"批次");
			}
		}

		if(null != examinationCostDO.getImportReturnState()){
			if ("1".equals(String.valueOf(examinationCostDO.getImportReturnState()))){
				return Result.fail("一个学校的考试批次返费只能导入一次，请勿重复导入！");
			}
		}


		if (null !=examinationCostDO.getUpdateExamine()){
			if (examinationCostDO.getUpdateExamine()!=1){ //表示市场总监修改了返费余额财务没有审核或者审核未通过
				return Result.fail("导入失败，修改后的返费余额财务审核未通过");
			}
		}


		//第二步--解析文件获得数据
		List<PublicRefundExportVO> list = EasyPoiUtil.importExcel(file, 10, 1, PublicRefundExportVO.class);


		for (int i=0;i<list.size();i++){
			if (list.get(i).getExpertName() == null && list.get(i).getExpertType() == null && list.get(i).getCost() == null){
				list.remove(i);
				i--;
			}
		}


		if (list.size() == 0) {
			return Result.fail("Excel数据为空，请完善");
		}

		examinationCostDO.setImportReturnState(1);

		for(int i=0;i<list.size();i++){
			List<ErrorCodeVo> list1 = new ArrayList<>();
			if(list1.size()>0){

				JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list1));
				//将list1存到缓存中
				EhCacheUtil.put("publicRefund" + this.getUserId(), jsonArray);
				return Result.build(EnumErrorCode.FileImportError.getCode(), EnumErrorCode.FileImportError.getMsg());
			}
		}

		//---------------------------------验证数据并导入-----------------------------------------
		//数据该表第三行，第一行标题，第二行表头
		int startRow = 3;
		int cout = 1;//错误信息计数
		List<PublicRefundDO> examCostList = new ArrayList<>();
		List<ErrorCodeVo> list1 = new ArrayList<>();
		List<PublicRefundExportVO> cerUtilsVos = new ArrayList<>();

		//插入前先去获取数据库最大批次号
		int batchId = publicRefundService.batchMax();

		//一批次费用合计
		BigDecimal total= new BigDecimal(0.0);

		List<PublicRefundExportVO> dsRefund = list.stream().filter(s -> s.getRefundType().equals("对私")).collect(Collectors.toList());

		double sum_dsRefund=0.00; //价格总和的初始值
		for (PublicRefundExportVO publicRefundExportVO : dsRefund) {
			sum_dsRefund+=Double.valueOf(publicRefundExportVO.getCost());//循环相加 集合中的总价格
		}

		List<PublicRefundExportVO> dgRefund = list.stream().filter(s -> s.getRefundType().equals("对公")).collect(Collectors.toList());

		double sum_dgRefund=0.00; //价格总和的初始值
		for (PublicRefundExportVO publicRefundExportVO : dgRefund) {
			sum_dgRefund+=Double.valueOf(publicRefundExportVO.getCost());//循环相加 集合中的总价格
		}

		sum_dgRefund = formatDouble4(sum_dgRefund);
		sum_dsRefund = formatDouble4(sum_dsRefund);

		if (sum_dgRefund != examinationCostDO.getReturnMoneySchool()){
			return Result.fail("导入失败,对公返款金额有误");
		}

		if (sum_dsRefund != examinationCostDO.getReturnMoneyPersonal()){
			return Result.fail("导入失败,对私返款金额有误");
		}


		for (int i = 0; i < list.size(); i++) {
			PublicRefundDO publicRefundDO=new PublicRefundDO();
			StringBuilder sb = new StringBuilder();
			if(StringUtils.isNullOrEmpty(list.get(i).getExpertName())){
				sb.append("第" + (i + startRow) + "专家姓名未填写，请完善！" + ";");
			}else if(StringUtils.isNullOrEmpty(list.get(i).getBankAccount())){
				sb.append("第" + (i + startRow) + "行银行账户未填写，请完善！" + ";");
			}else if(StringUtils.isNullOrEmpty(list.get(i).getBankOfDeposit())){
				sb.append("第" + (i + startRow) + "行开户银行未填写，请完善！" + ";");
			} else if(StringUtils.isNullOrEmpty(list.get(i).getMobile())){
				sb.append("第" + (i + startRow) + "手机号未填写，请完善！" + ";");
			} else if(StringUtils.isNullOrEmpty(list.get(i).getExpertIdCard())){
				sb.append("第" + (i + startRow) + "身份证号码未填写，请完善！" + ";");
			}
			if (sb.length() > 0) {
				ErrorCodeVo errorCodeVo = new ErrorCodeVo();
				errorCodeVo.setSeq(cout);
				cout += 1;
				errorCodeVo.setErrorCode(sb.toString());
				list1.add(errorCodeVo);
			}
			//批次id
			publicRefundDO.setBatchId(batchId);
			//银行账户
			publicRefundDO.setBankAccount(list.get(i).getBankAccount());
			//默认是未确定未退回的数据
			publicRefundDO.setConfirmState(0);
			publicRefundDO.setReturnState(0);
			//对公对私费用
			publicRefundDO.setCost(new BigDecimal(list.get(i).getCost()));
			//考试费表ID
			publicRefundDO.setExamFeeIf(examFeeIf);
			//姓名
			publicRefundDO.setExpertName(list.get(i).getExpertName());
			//专家类型
			publicRefundDO.setExpertType(list.get(i).getExpertType());
			//备注
			publicRefundDO.setRemarks(list.get(i).getRemarks());
			//手机号
			publicRefundDO.setMobile(list.get(i).getMobile());
			//0对公 1对私
			String refundType = "0";
			if ("对私".equals(list.get(i).getRefundType())){
				refundType="1";
			}
			publicRefundDO.setRefundType(refundType);
			//开户银行
			publicRefundDO.setBankOfDeposit(list.get(i).getBankOfDeposit());
			//年份
			publicRefundDO.setYear(year);

			if(examinationCostDO!=null){
				//考评学校
				publicRefundDO.setEvaluationSchool(examinationCostDO.getOrgName()!=null?examinationCostDO.getOrgName():null);
				//考评日期
				publicRefundDO.setEvaluationTime(examinationCostDO.getExaminationDate());
				//考评人数
				publicRefundDO.setEvaluationNum(examinationCostDO.getStuNum());
			}
			//录入时间
			publicRefundDO.setCreateTime(new Date());
			//是否返费，默认未返费
			publicRefundDO.setRefundRrNot(0);
			publicRefundDO.setExpertIdCard(list.get(i).getExpertIdCard()); //专家身份证号码
			examCostList.add(publicRefundDO);
		}


//		if(list.size()!=examCostList.size()){ //如果解析Excel得到的数据与要新增的数据不同则新增失败！
//			return Result.fail("数据有误，导入失败！");
//		}



		if (list1.size() == 0) {
			publicRefundService.insertBatch(examCostList);
			examinationCostService.updateById(examinationCostDO);
			return Result.ok("成功导入" + list.size() + "条数据");
		}else{
			JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list1));
			//将list1存到缓存中
			EhCacheUtil.put("publicRefund" + this.getUserId(), jsonArray);
			return Result.build(EnumErrorCode.FileImportError.getCode(), EnumErrorCode.FileImportError.getMsg());
		}
	}



	//		for (int i =0;i<list.size();i++){
//			BaseDO baseDO =new BaseDO();
//			baseDO.setBaseBatch("02");
//			String expertIdCard = list.get(i).getExpertName();
//			if (Integer.valueOf(expertIdCard) >9){
//				baseDO.setBaseCode("0"+expertIdCard);
//			}else if (Integer.valueOf(expertIdCard) >99){
//				baseDO.setBaseCode(expertIdCard);
//			}else {
//				baseDO.setBaseCode("00"+expertIdCard);
//			}
//			baseDO.setBaseName(list.get(i).getExpertIdCard().trim());
//			jzbaseService.insert(baseDO);
//		}



//	public static double formatDouble(double d){
//		//新的方法，如果不需要四舍五入可以使用RoundingMode.DOWN
//		BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.UP);
//		return bd.doubleValue();
//	}

	public double formatDouble4(double d){
		DecimalFormat df = new DecimalFormat("#.00");
		return Double.valueOf(df.format(d));
	}



	/**
	 * 选择导出判断
	 *
	 */
	@RequestMapping("/refndExcelRole")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	@Transactional
	public Result<?> refndExcelRole(@RequestParam("ids[]") String[] ids) {
		List ids1=new ArrayList();
		Long roleId=0l;
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName

		for(String ll:ids){

			ids1.add(ll);
		}
		List<PublicRefundDO> list = new ArrayList<>();
		Map map=new HashMap();

		map.put("export",0);
		map.put("list",ids1);

		list=	publicRefundService.listPublicRefunds(map);

		//如果批次id不同不能导出
		Integer batchId =0;
		if (null!=list){
			batchId = list.get(0).getBatchId();
		}
		for (PublicRefundDO publicRefundDO:list){
			if (!batchId.equals(publicRefundDO.getBatchId())){
				return Result.fail("导出时必须选择同一批次！");
			}
		}

		for (int i=0;i<list.size();i++){
			if (list.get(i).getExport() == 1){
				return Result.fail("该数据已被导出");
			}
		}


		//导出操作
		return  Result.ok();
	}



	@Log("返费批量退回")
	@PostMapping("/returnBatchCost")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:returnBatchCost")
	public Result<String>  rejectCost(@RequestParam("ids[]") String[] ids,@RequestParam("returnExplain") String returnExplain){

		if (ids.length<1){
			return Result.build(500,"未选择数据");
		}

		List<PublicRefundDO> updateRefundDO =new ArrayList<>();
		for (int i=0;i < ids.length;i++){
			List<PublicRefundDO> ref = publicRefundService.selectBatchIds(Arrays.asList(ids));

			if (ref.get(i).getConfirmState()==0){
				return Result.fail("请先确认后再进行退回操作！");
			}

			if (1==ref.get(i).getRefundRrNot()){
				return Result.fail("已返费数据无法退回！");
			}
			if (2==ref.get(i).getRefundRrNot()){
				return Result.fail("请勿重复退回！");
			}

			PublicRefundDO publicRefundDO =new PublicRefundDO();
			publicRefundDO.setId(Long.valueOf(ids[i])).setRefundRrNot(2).setReturnDate(new Date()).setReturnState(1).setReturnExplain("批量退回原因："+returnExplain).setConfirmState(0).setExport(0);
			updateRefundDO.add(publicRefundDO);
		}
		boolean ret = publicRefundService.updateBatchById(updateRefundDO);
		if (!ret){
			return Result.fail();
		}

		logService.saveOperationLog(new LogBO().setOperation("2022返费信息管理/返费批量退回").setUserId(String.valueOf(this.getUserId())));

		return Result.ok();
	}





//	/**
//	 * 选择导出
//	 *
//	 */
//	@RequestMapping("/refndExcel")
//	@RequiresPermissions("jzweb:publicRefund:import")
//	@ResponseBody
//	@Transactional
//	public void refndExcel(@RequestParam("ids[]") Long[] ids,HttpServletResponse response) {
//		List ids1=new ArrayList();
//		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
//		//获取用户系统权限
//		Integer is = userRoleService.userRolenum(this.getUserId());
//
//		for(Long ll:ids){
//			ids1.add(ll);
//		}
//		List<PublicRefundDO> list = new ArrayList<>();
//		List<PublicRefundExportVO> listFile = new LinkedList<>();
//		Map map=new HashMap();
//
//		map.put("export",0);
//		map.put("list",ids1);
//
//		list=publicRefundService.listPublicRefunds(map);
//
//		for(PublicRefundDO li:list){
//			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//			PublicRefundExportVO publicRefundExportVO=new PublicRefundExportVO();
//			publicRefundExportVO.setBatchId(li.getBatchId());
//			publicRefundExportVO.setExpertName(li.getExpertName());
//			publicRefundExportVO.setExpertType(li.getExpertType());
//			publicRefundExportVO.setEvaluationTime(sdf.format(li.getEvaluationTime()));
//			publicRefundExportVO.setEvaluationSchool(li.getEvaluationSchool());
//			publicRefundExportVO.setProvince(li.getProvince());
//			publicRefundExportVO.setRegionUserName(li.getRegionUserName());
//			publicRefundExportVO.setLaborCost(li.getLaborCost());
//			publicRefundExportVO.setMeals(li.getMeals());
//			publicRefundExportVO.setTrafficExpense(li.getTrafficExpense());
//			publicRefundExportVO.setHotelExpense(li.getHotelExpense());
//			publicRefundExportVO.setPostage(li.getPostage());
//			publicRefundExportVO.setTrafficExpenseMax(li.getTrafficExpenseMax());
//			publicRefundExportVO.setSumExpense(li.getSumExpense());
//			publicRefundExportVO.setBankAccount(li.getBankAccount());
//			publicRefundExportVO.setBankOfDeposit(li.getBankOfDeposit());
//			publicRefundExportVO.setRemarks(li.getRemarks());
//			publicRefundExportVO.setEvaluationNum(li.getEvaluationNum());
//			publicRefundExportVO.setLaborCostAveragePrice(li.getLaborCostAveragePrice());
//			//其它费用
//			publicRefundExportVO.setOtherCost(li.getOtherCost());
//			listFile.add(publicRefundExportVO);
//			if(is==8||is==9){
//				li.setExport(1);
//				li.setDeriveTime(new Date());
//				publicRefundService.updateById(li);
//			}
//
//		}
//
//		ExportParams exportParams =new ExportParams();
//		//定义开销总数对象
//		ExpenseTotalBO expenseTotalBO = publicRefundService.moneyTotal(map);
//		expenseTotalBO = bigDecimalDef(expenseTotalBO);
//		exportParams.setSecondTitle(msgFormat(expenseTotalBO));
//		EasyPoiUtil.exportExcel2(listFile, "考试费返费信息表", "返费信息（选择）", PublicRefundExportVO.class, "考试费返费信息（选择）", response,exportParams);
//	}
//









	/**
	 * 选择导出
	 *
	 */
	@RequestMapping("/countZxslExcel")
	@ResponseBody
	@Transactional
	public void countZxslExcel(HttpServletResponse response) {

		List<CountZxslVO> countZxslVOS =new ArrayList<>();

		List<OrganizationNewsDO> organizationNewsDOS = organizationNewsService.selectList(null); //查询出所有学校
		for (int i = 0;i<organizationNewsDOS.size();i++){
			CountZxslVO countZxslVO =new CountZxslVO();
			countZxslVO.setName(organizationNewsDOS.get(i).getName());

			EntityWrapper<ConsultationsDO> wrapper =new EntityWrapper();
			wrapper.like("contents",countZxslVO.getName());
			int count = consultationsService.selectCount(wrapper);
			countZxslVO.setCount(count);

			countZxslVOS.add(countZxslVO);
		}

		EasyPoiUtil.exportExcel(countZxslVOS, "各学校资讯数量统计", "各学校资讯数量统计", CountZxslVO.class, "2022考试费返费信息详情导入模板", response);
	}










	/**
	 * 选择导出
	 *
	 */
	@RequestMapping("/refndExcel")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	@Transactional
	public void refndExcel(@RequestParam("ids[]") Long[] ids,HttpServletResponse response) throws Exception {
		List ids1=new ArrayList();
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
		//获取用户系统权限
		Integer is = userRoleService.userRolenum(this.getUserId());

		for(Long ll:ids){
			ids1.add(ll);
		}
		List<PublicRefundDO> list = new ArrayList<>();
		List<PublicRefundExportVO> listFile = new LinkedList<>();
		Map map=new HashMap();

		map.put("export",0);
		map.put("list",ids1);

		list=publicRefundService.listPublicRefunds(map);

		for(PublicRefundDO li:list){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			PublicRefundExportVO publicRefundExportVO=new PublicRefundExportVO();
			publicRefundExportVO.setBatchId(li.getBatchId());
			publicRefundExportVO.setExpertName(li.getExpertName());
			publicRefundExportVO.setExpertType(li.getExpertType());
			publicRefundExportVO.setEvaluationTime(sdf.format(li.getEvaluationTime()));
			publicRefundExportVO.setEvaluationSchool(li.getEvaluationSchool());
			publicRefundExportVO.setProvince(li.getProvince());
			publicRefundExportVO.setRegionUserName(li.getRegionUserName());
			publicRefundExportVO.setBankAccount(li.getBankAccount());
			publicRefundExportVO.setBankOfDeposit(li.getBankOfDeposit());
			publicRefundExportVO.setRemarks(li.getRemarks());
			publicRefundExportVO.setRefundType(li.getRefundType());
			publicRefundExportVO.setEvaluationNum(li.getEvaluationNum());
			publicRefundExportVO.setCost(String.valueOf(li.getCost()));
			publicRefundExportVO.setExpertIdCard(li.getExpertIdCard());
			publicRefundExportVO.setMobile(li.getMobile());
			listFile.add(publicRefundExportVO);
			if(is==8||is==9||is==18||is==19){
				li.setExport(1);
				li.setDeriveTime(new Date());
				publicRefundService.updateById(li);
			}

		}
		//定义开销总数对象
		PublicAndPrivateExpesetotalBo publicAndPrivateExpesetotalBo = publicRefundService.moneyTotal(map);

		ExaminationCostDO examinationCostDO=examinationCostService.selectById(list.get(0).getExamFeeIf());

		String filename = "2022考试费返费信息表(批量).docx";


		documentWord(response,publicAndPrivateExpesetotalBo,listFile,filename,examinationCostDO);
	}






//
//	private File kssbTemplateFile(){
//		File templateFile= new File(RootPath("/easypoi/kssbb.docx"));
//
//		System.out.printf("++++++++++++++++++++++++++++++++++++++++++++++"+templateFile.getPath());
//
//		return templateFile;
//	}
//
//	private void documentWordKssbb(HttpServletResponse response,String filename) throws Exception {
//		//导出组装数据
//		Map<String,Object> params =new HashMap<>();
//		params.put("sbrxm","张三");
//		params.put("xb","男");
//		params.put("csrq", "1980-01-20");
//		params.put("xlzy", "计算机应用"); //应返款
//		params.put("zjlx","身份证");//学校占比
//		params.put("sfzh","41035551465955545");//学校返款金额
//		params.put("sjhm","1763025245");//个人占比
//		params.put("gzdw","金职伟业集团有限公司");//个人返款金额
//		params.put("zy","师资");
//		params.put("dj","高级师资");
//		params.put("zsbh","JZ642654225");
//		params.put("nian","2023");
//		params.put("yue","03");
//		params.put("ri","08");
//		params.put("fzjg","湖南金职伟业集团有限公司");
//		params.put("zczsmc","幼儿照护");
//		params.put("zczsdj","高级");
//		params.put("zczsbh","JZ642654225");
//		params.put("fznian","2023");
//		params.put("fzyue","03");
//		params.put("fzri","08");
//		params.put("fzjg","发证书的机构");
//		params.put("sbzy","育婴员");
//		params.put("sbdj","高级");
//		params.put("csgw","保姆");
//		params.put("ljgz","5");
//		params.put("sbdh","申报");
//		params.put("zjgz","没有工种");
//		params.put("gzdw","阿里巴巴");
//		params.put("lxdh","155654455");
//
//		XWPFDocument word = WordExportUtil.exportWord07(kssbTemplateFile().getPath(),params);
//		response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));
//		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//		word.write(response.getOutputStream());
//
//	}










	/**
	 * 下载全部返费信息 Excel
	 *
	 * @param response
	 */
	@RequestMapping("/exportFileExcel")
	@RequiresPermissions("jzweb:publicRefund:exportFileExcel")
	@ResponseBody
	public void exportFileExcel(HttpServletResponse response,PublicRefundVO publicRefundVO) {
		Map map=new HashMap();
		map.put("year",publicRefundVO.getYear());
		map.put("batchId",publicRefundVO.getBatchId());
		map.put("batchId2",publicRefundVO.getBatchId2());
		map.put("refundType",1); //仅导出对私部分

		List<PublicRefundDO> list = publicRefundService.listPublicRefunds(map);

		Integer batchIdVal = list.get(0).getBatchId(); //获取第一个batchId的值
		String fzrVal = list.get(0).getRegionUserName(); //获取第一个区域负责人名称
		BigDecimal batchTotalCost = new BigDecimal(0.0); //一批次费用的总金额

		for (int i=0;i<list.size();i++){
			if (batchIdVal.equals(list.get(i).getBatchId())){
				list.get(i).setTatolCost("");
				list.get(i).setRegionUserName("");
				batchTotalCost = batchTotalCost.add(list.get(i).getCost());
			}else {
				list.get(i-1).setTatolCost(String.valueOf(batchTotalCost));
				list.get(i-1).setRegionUserName(fzrVal);
				batchTotalCost = new BigDecimal(0.0); //清空
				fzrVal = list.get(i).getRegionUserName(); //当前负责人名称
				batchIdVal = list.get(i).getBatchId(); //当前的批次id
				i--;
			}
		}
		list.get(list.size()-1).setTatolCost(String.valueOf(batchTotalCost));
		list.get(list.size()-1).setRegionUserName(fzrVal);

	List<PublicRefundExportSiVO> listFile =new ArrayList<>();
		for (int i=0;i<list.size();i++){
			PublicRefundExportSiVO publicRefundExportSiVO =new PublicRefundExportSiVO();
			publicRefundExportSiVO.setBatchId(list.get(i).getBatchId());
			publicRefundExportSiVO.setExpertName(list.get(i).getExpertName());
			publicRefundExportSiVO.setEvaluationSchool(list.get(i).getEvaluationSchool());
			publicRefundExportSiVO.setBankAccount(list.get(i).getBankAccount());
			publicRefundExportSiVO.setBankOfDeposit(list.get(i).getBankOfDeposit());
			publicRefundExportSiVO.setCost(String.valueOf(list.get(i).getCost()));
			publicRefundExportSiVO.setTotalCost(list.get(i).getTatolCost());
			publicRefundExportSiVO.setRegionUserName(list.get(i).getRegionUserName());
			publicRefundExportSiVO.setExpertIdCard(list.get(i).getExpertIdCard());
			publicRefundExportSiVO.setRemark(list.get(i).getRemarks());
			publicRefundExportSiVO.setMobile(list.get(i).getMobile());
			listFile.add(publicRefundExportSiVO);
		}
		EasyPoiUtil.exportExcel(listFile, "财务对私打款(返费)", "财务对私打款(返费)", PublicRefundExportSiVO.class, "财务对私打款(返费)", response);
	}



	/**
	 * 下载全部返费信息 Excel
	 *
	 * @param response
	 */
	@RequestMapping("/exportFileExcel2")
	@RequiresPermissions("jzweb:publicRefund:exportFileExcel2")
	@ResponseBody
	public void exportFileExcel2(HttpServletResponse response,PublicRefundVO publicRefundVO) {
		Map map=new HashMap();
		map.put("batchId",publicRefundVO.getBatchId());
		map.put("batchId2",publicRefundVO.getBatchId2());

		List<PublicRefundDO> list = publicRefundService.listPublicRefunds(map);

		Integer batchIdVal = list.get(0).getBatchId(); //获取第一个batchId的值
		String fzrVal = list.get(0).getRegionUserName(); //获取第一个区域负责人名称
		BigDecimal batchTotalCost = new BigDecimal(0.0); //一批次费用的总金额

		for (int i=0;i<list.size();i++){
			if (batchIdVal.equals(list.get(i).getBatchId())){
				list.get(i).setTatolCost("");
				list.get(i).setRegionUserName("");
				batchTotalCost = batchTotalCost.add(list.get(i).getCost());
			}else {
				list.get(i-1).setTatolCost(String.valueOf(batchTotalCost));
				list.get(i-1).setRegionUserName(fzrVal);
				batchTotalCost = new BigDecimal(0.0); //清空
				fzrVal = list.get(i).getRegionUserName(); //当前负责人名称
				batchIdVal = list.get(i).getBatchId(); //当前的批次id
				i--;
			}
		}
		list.get(list.size()-1).setTatolCost(String.valueOf(batchTotalCost));
		list.get(list.size()-1).setRegionUserName(fzrVal);

		List<PublicRefundExportSiVO> listFile =new ArrayList<>();
		for (int i=0;i<list.size();i++){
			PublicRefundExportSiVO publicRefundExportSiVO =new PublicRefundExportSiVO();
			publicRefundExportSiVO.setBatchId(list.get(i).getBatchId());
			publicRefundExportSiVO.setExpertName(list.get(i).getExpertName());
			publicRefundExportSiVO.setEvaluationSchool(list.get(i).getEvaluationSchool());
			publicRefundExportSiVO.setBankAccount(list.get(i).getBankAccount());
			publicRefundExportSiVO.setBankOfDeposit(list.get(i).getBankOfDeposit());
			publicRefundExportSiVO.setCost(String.valueOf(list.get(i).getCost()));
			publicRefundExportSiVO.setTotalCost(list.get(i).getTatolCost());
			publicRefundExportSiVO.setRegionUserName(list.get(i).getRegionUserName());
			publicRefundExportSiVO.setExpertIdCard(list.get(i).getExpertIdCard());
			publicRefundExportSiVO.setRemark(list.get(i).getRemarks());
			listFile.add(publicRefundExportSiVO);
		}
		EasyPoiUtil.exportExcel(listFile, "全部返费Excel", "全部返费Excel", PublicRefundExportSiVO.class, "全部返费Excel", response);
	}























//	/**
//	 * 下载全部返费信息
//	 *
//	 * @param response
//	 */
//	@RequestMapping("/exportFile")
//	@RequiresPermissions("jzweb:publicRefund:import")
//	@ResponseBody
//	public void exportFile(HttpServletResponse response,PublicRefundVO publicRefundVO) {
//		Integer is = userRoleService.userRolenum(this.getUserId());
//		List<RoleDO>  listRole=roleService.findAll();
//		List<PublicRefundDO> list = new LinkedList<>();
//		List<PublicRefundExportVO> listFile = new LinkedList<>();
//		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
//		//查询所有用户所属权限
//
//		Map map=new HashMap();
//		Long chOO1=0l;
//		Long chOO2=0l;
//		if(is==6){
//			List lists=new ArrayList();
//			List list1s=new ArrayList();
//			for(RoleDO roleDO:listRole){
//				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
//					chOO1=roleDO.getId();
//					lists.add(chOO1);
//					//break;
//				}
//			}
//			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();
//
//			wrapper1.in("roleId",lists);
//			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
//			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
//			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
//			for(UserRoleDO users:userRoleDOS){
//				list1s.add(users.getUserId());
//			}
//			map.put("list1",list1s);
//		}else if(is==7){
//			List lists=new ArrayList();
//			List list1s=new ArrayList();
//			for(RoleDO roleDO:listRole){
//				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
//					chOO1=roleDO.getId();
//					lists.add(chOO1);
//					//break;
//				}
//			}
//			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();
//
//			wrapper1.in("roleId",lists);
//			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
//			for(UserRoleDO users:userRoleDOS){
//				list1s.add(users.getUserId());
//			}
//			map.put("list1",list1s);
//		}else if(is==8){
//			map.put("list2",this.getUserId().toString());
//
//		}else if(is==9){
//
//			map.put("list2",this.getUserId().toString());
//		}
//
//		map.put("year",publicRefundVO.getYear());
//		map.put("province",publicRefundVO.getProvince());
//		map.put("refundRrNot",publicRefundVO.getRefundRrNot());
//		map.put("batchId",publicRefundVO.getBatchId());
//		map.put("batchId2",publicRefundVO.getBatchId2());
//		map.put("orgName",publicRefundVO.getSchoolName());
//		map.put("regionUserName",publicRefundVO.getRegionUserName());
//		map.put("examStartDate",StringUtil.notEmpty(publicRefundVO.getExamStartDate())?publicRefundVO.getExamStartDate()+" 00:00:00":publicRefundVO.getExamStartDate());
//		map.put("examEndDate",StringUtil.notEmpty(publicRefundVO.getExamEndDate())?publicRefundVO.getExamEndDate()+" 00:00:00":publicRefundVO.getExamEndDate());
//		map.put("export",0);
//		map.put("list",null);
//		list=	publicRefundService.listPublicRefunds(map);
//
//
//		for(PublicRefundDO li:list){
//			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//			PublicRefundExportVO publicRefundExportVO=new PublicRefundExportVO();
//			publicRefundExportVO.setBatchId(li.getBatchId());
//			publicRefundExportVO.setExpertName(li.getExpertName());
//			publicRefundExportVO.setExpertType(li.getExpertType());
//			publicRefundExportVO.setEvaluationTime(sdf.format(li.getEvaluationTime()));
//			publicRefundExportVO.setEvaluationSchool(li.getEvaluationSchool());
//			publicRefundExportVO.setProvince(li.getProvince());
//			publicRefundExportVO.setRegionUserName(li.getRegionUserName());
//			publicRefundExportVO.setLaborCost(li.getLaborCost());
//			publicRefundExportVO.setMeals(li.getMeals());
//			publicRefundExportVO.setTrafficExpense(li.getTrafficExpense());
//			publicRefundExportVO.setHotelExpense(li.getHotelExpense());
//			publicRefundExportVO.setPostage(li.getPostage());
//			publicRefundExportVO.setTrafficExpenseMax(li.getTrafficExpenseMax());
//			publicRefundExportVO.setSumExpense(li.getSumExpense());
//			publicRefundExportVO.setBankAccount(li.getBankAccount());
//			publicRefundExportVO.setBankOfDeposit(li.getBankOfDeposit());
//			publicRefundExportVO.setRemarks(li.getRemarks());
//			publicRefundExportVO.setEvaluationNum(li.getEvaluationNum());
//			publicRefundExportVO.setLaborCostAveragePrice(li.getLaborCostAveragePrice());
//			//其它费用
//			publicRefundExportVO.setOtherCost(li.getOtherCost());
//			listFile.add(publicRefundExportVO);
//			if(is==8||is==9){
//				li.setExport(1);
//				li.setDeriveTime(new Date());
//				publicRefundService.updateById(li);
//			}
//		}
//		ExportParams exportParams =new ExportParams();
//		ExpenseTotalBO expenseTotalBO = publicRefundService.moneyTotal(map);
//		expenseTotalBO = bigDecimalDef(expenseTotalBO);
//		exportParams.setSecondTitle(msgFormat(expenseTotalBO));
//
//		EasyPoiUtil.exportExcel2(listFile, "考试费劳务费信息表", "劳务费信息（批量）", PublicRefundExportVO.class, "考试费劳务费信息（批量）", response,exportParams);
//	}
//







	/**
	 * 下载全部返费信息
	 *
	 * @param response
	 */
	@RequestMapping("/exportFile")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	public void exportFile(HttpServletResponse response,PublicRefundVO publicRefundVO) throws Exception {
		Integer is = userRoleService.userRolenum(this.getUserId());
		List<RoleDO>  listRole=roleService.findAll();
		List<PublicRefundDO> list = new LinkedList<>();
		List<PublicRefundExportVO> listFile = new LinkedList<>();
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
		//查询所有用户所属权限

		Map map=new HashMap();
		Long chOO1=0l;
		Long chOO2=0l;
		if(is==6){
			List lists=new ArrayList();
			List list1s=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
			for(UserRoleDO users:userRoleDOS){
				list1s.add(users.getUserId());
			}
			map.put("list1",list1s);
		}else if(is==7){
			List lists=new ArrayList();
			List list1s=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			for(UserRoleDO users:userRoleDOS){
				list1s.add(users.getUserId());
			}
			map.put("list1",list1s);
		}else if(is==8){
			map.put("list2",this.getUserId().toString());

		}else if(is==9){

			map.put("list2",this.getUserId().toString());
		}else if(is==18){

			map.put("list2",this.getUserId().toString());
		}else if(is==19){

			map.put("list2",this.getUserId().toString());
		}

		map.put("year",publicRefundVO.getYear());
		map.put("refundType",publicRefundVO.getRefundType());
		map.put("province",publicRefundVO.getProvince());
		map.put("refundRrNot",publicRefundVO.getRefundRrNot());
		map.put("batchId",publicRefundVO.getBatchId());
		map.put("batchId2",publicRefundVO.getBatchId2());
		map.put("orgName",publicRefundVO.getSchoolName());
		map.put("regionUserName",publicRefundVO.getRegionUserName());
		map.put("evaluationSchool",publicRefundVO.getSchoolName());
		map.put("examStartDate",StringUtil.notEmpty(publicRefundVO.getExamStartDate())?publicRefundVO.getExamStartDate()+" 00:00:00":publicRefundVO.getExamStartDate());
		map.put("examEndDate",StringUtil.notEmpty(publicRefundVO.getExamEndDate())?publicRefundVO.getExamEndDate()+" 00:00:00":publicRefundVO.getExamEndDate());
		map.put("export",0);
		map.put("list",null);
		list=	publicRefundService.listPublicRefunds(map);


		for(PublicRefundDO li:list){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			PublicRefundExportVO publicRefundExportVO=new PublicRefundExportVO();
			publicRefundExportVO.setBatchId(li.getBatchId());
			publicRefundExportVO.setExpertName(li.getExpertName());
			publicRefundExportVO.setExpertType(li.getExpertType());
			publicRefundExportVO.setEvaluationTime(sdf.format(li.getEvaluationTime()));
			publicRefundExportVO.setEvaluationSchool(li.getEvaluationSchool());
			publicRefundExportVO.setProvince(li.getProvince());
			publicRefundExportVO.setRegionUserName(li.getRegionUserName());
			publicRefundExportVO.setBankAccount(li.getBankAccount());
			publicRefundExportVO.setBankOfDeposit(li.getBankOfDeposit());
			publicRefundExportVO.setRemarks(li.getRemarks());
			publicRefundExportVO.setEvaluationNum(li.getEvaluationNum());
			publicRefundExportVO.setRefundType(String.valueOf(li.getRefundType()));
			publicRefundExportVO.setCost(String.valueOf(li.getCost()));
			//其它费用
			listFile.add(publicRefundExportVO);
			if(is==8||is==9||is==18||is==19){
				if (null == publicRefundVO.getRefundType()){
					li.setExport(1);
				}
				li.setDeriveTime(new Date());
				if (publicRefundVO.getRefundType()!=null&& 0 == publicRefundVO.getRefundType()){
					li.setPublicExportState(1);
					li.setExport(1);
				}else if (publicRefundVO.getRefundType()!=null&& 1 == publicRefundVO.getRefundType()){
					li.setPrivateExportState(1);
					li.setExport(1);
				}
				publicRefundService.updateById(li);
			}
		}
		PublicAndPrivateExpesetotalBo publicAndPrivateExpesetotalBo = publicRefundService.moneyTotal(map);

		ExaminationCostDO examinationCostDO=examinationCostService.selectById(list.get(0).getExamFeeIf());


		String filename = "2022考试费返费信息表(全量).docx";

		if (publicRefundVO.getRefundType()==null){
			documentWord(response,publicAndPrivateExpesetotalBo,listFile,filename,examinationCostDO);
		}
		if (publicRefundVO.getRefundType()!=null){
			documentWord(response,publicAndPrivateExpesetotalBo,listFile,filename,examinationCostDO,publicRefundVO.getRefundType());
		}
	}




	private void documentWord(HttpServletResponse response,PublicAndPrivateExpesetotalBo
			publicAndPrivateExpesetotalBo,List<PublicRefundExportVO> listFile,String filename,ExaminationCostDO examinationCostDO) throws Exception{

		List<PublicRefundExportVO> rstListFile = listFile.stream().filter(list -> "1".equals(list.getRefundType())).collect(Collectors.toList());

		Date date=new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

		//导出组装数据
		Map<String,Object> params =new HashMap<>();

		BigDecimal typePublic = publicAndPrivateExpesetotalBo.getTypePublic();//获取对公返费金额
		int is = typePublic.compareTo(new BigDecimal(0)); //判断是否为0

		params.put("wordEvaluationSchool",listFile.get(0).getEvaluationSchool());
		params.put("wordEvTime",listFile.get(0).getEvaluationTime());
		params.put("wordEvNum", String.valueOf(listFile.get(0).getEvaluationNum()));
		params.put("wordSumExpenseTotal", examinationCostDO.getReturnMoney()); //应返款
		params.put("schoolZb",examinationCostDO.getReturnMoneySchoolRatio());//学校占比
		params.put("school",String.valueOf(publicAndPrivateExpesetotalBo.getTypePublic()));//学校返款金额
		params.put("personalZb",examinationCostDO.getReturnMoneyPersonalRatio());//个人占比
		params.put("personal",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));//个人返款金额
		params.put("wordExpertName",is==0?"无需对公返款信息":listFile.get(0).getExpertName());
		params.put("wordUnitName","湖南金职伟业母婴护理有限公司");
		params.put("wordApplyTime",sdf.format(date));
		params.put("wordBankAccount",is==0?"无需对公返款信息":listFile.get(0).getBankAccount());
		params.put("wordBankOfDeposit",is==0?"无需对公返款信息":listFile.get(0).getBankOfDeposit());
		params.put("batchId",listFile.get(0).getBatchId());
		params.put("wordSum",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));

		for (int i=0;i<rstListFile.size();i++){
			rstListFile.get(i).setShowId(String.valueOf((int)i+1));
		}

		params.put("listFile",rstListFile);

		XWPFDocument word = WordExportUtil.exportWord07(templateFile().getPath(),params);
		response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		word.write(response.getOutputStream());
	}



	private void documentWord(HttpServletResponse response,PublicAndPrivateExpesetotalBo
			publicAndPrivateExpesetotalBo,List<PublicRefundExportVO> listFile,String filename,ExaminationCostDO examinationCostDO,Integer type) throws Exception{

		List<PublicRefundExportVO> rstListFile = listFile.stream().filter(list -> "1".equals(list.getRefundType())).collect(Collectors.toList());

		Date date=new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

		//导出组装数据
		Map<String,Object> params =new HashMap<>();
		if (type ==0){  //对公导出
			params.put("wordEvaluationSchool",listFile.get(0).getEvaluationSchool());
			params.put("wordEvTime",listFile.get(0).getEvaluationTime());
			params.put("wordEvNum", String.valueOf(listFile.get(0).getEvaluationNum()));
			params.put("wordSumExpenseTotal", examinationCostDO.getReturnMoney()); //应返款
			params.put("schoolZb",examinationCostDO.getReturnMoneySchoolRatio());//学校占比
			params.put("school",String.valueOf(publicAndPrivateExpesetotalBo.getTypePublic()));//学校返款金额
			params.put("personalZb",examinationCostDO.getReturnMoneyPersonalRatio());//个人占比
			params.put("personal",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));//个人返款金额
			params.put("wordExpertName",listFile.get(0).getExpertName());
			params.put("wordUnitName","湖南金职伟业母婴护理有限公司");
			params.put("wordApplyTime",sdf.format(date));
			params.put("wordBankAccount",listFile.get(0).getBankAccount());
			params.put("wordBankOfDeposit",listFile.get(0).getBankOfDeposit());
			params.put("batchId",listFile.get(0).getBatchId());
			params.put("wordSum",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));
		}
		if (type == 1){
			params.put("wordEvaluationSchool",listFile.get(0).getEvaluationSchool());
			params.put("wordEvTime",listFile.get(0).getEvaluationTime());
			params.put("wordEvNum", String.valueOf(listFile.get(0).getEvaluationNum()));
			params.put("wordSumExpenseTotal", examinationCostDO.getReturnMoney()); //应返款
			params.put("schoolZb",examinationCostDO.getReturnMoneySchoolRatio());//学校占比
			params.put("school","0");//学校返款金额
			params.put("personalZb",examinationCostDO.getReturnMoneyPersonalRatio());//个人占比
			params.put("personal",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));//个人返款金额
			params.put("wordExpertName","对私导出暂无账户名");
			params.put("wordUnitName","湖南金职伟业母婴护理有限公司");
			params.put("wordApplyTime",sdf.format(date));
			params.put("wordBankAccount","对私导出暂无对公账户");
			params.put("wordBankOfDeposit","对私导出暂无对公开户行");
			params.put("batchId",listFile.get(0).getBatchId());
			params.put("wordSum",String.valueOf(publicAndPrivateExpesetotalBo.getTypePrivate()));
		}

		for (int i=0;i<rstListFile.size();i++){
			rstListFile.get(i).setShowId(String.valueOf((int)i+1));
		}

		params.put("listFile",rstListFile);

		XWPFDocument word = WordExportUtil.exportWord07(templateFile().getPath(),params);
		response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		word.write(response.getOutputStream());
	}




	/**
	 * 全部导出判断
	 *
	 */
	@RequestMapping("/exportFileRole")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	@Transactional
	public Result<?> exportFileRole(PublicRefundVO publicRefundVO) {
		Integer is = userRoleService.userRolenum(this.getUserId());
		List<RoleDO>  listRole=roleService.findAll();
		List<PublicRefundDO> list = new LinkedList<>();
		List<PublicRefundExportVO> listFile = new LinkedList<>();
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
		//查询所有用户所属权限

		Map map=new HashMap();
		Long chOO1=0l;
		Long chOO2=0l;
		if(is==6){
			List lists=new ArrayList();
			List list1s=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
			for(UserRoleDO users:userRoleDOS){
				list1s.add(users.getUserId());
			}
			map.put("list1",list1s);
		}else if(is==7){
			List lists=new ArrayList();
			List list1s=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
					chOO1=roleDO.getId();
					lists.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			for(UserRoleDO users:userRoleDOS){
				list1s.add(users.getUserId());
			}
			map.put("list1",list1s);
		}else if(is==8){
			map.put("list2",this.getUserId().toString());

		}else if(is==9){

			map.put("list2",this.getUserId().toString());
		}else if(is==18){

			map.put("list2",this.getUserId().toString());
		}else if(is==19){

			map.put("list2",this.getUserId().toString());
		}

		map.put("year",publicRefundVO.getYear());
		map.put("refundType",publicRefundVO.getRefundType());
		map.put("province",publicRefundVO.getProvince());
		map.put("refundRrNot",publicRefundVO.getRefundRrNot());
		map.put("batchId",publicRefundVO.getBatchId());
		map.put("batchId2",publicRefundVO.getBatchId2());
		map.put("orgName",publicRefundVO.getSchoolName());
		map.put("regionUserName",publicRefundVO.getRegionUserName());
		map.put("examStartDate",StringUtil.notEmpty(publicRefundVO.getExamStartDate())?publicRefundVO.getExamStartDate()+" 00:00:00":publicRefundVO.getExamStartDate());
		map.put("examEndDate",StringUtil.notEmpty(publicRefundVO.getExamEndDate())?publicRefundVO.getExamEndDate()+" 00:00:00":publicRefundVO.getExamEndDate());
		map.put("export",0);
		map.put("list",null);
		list=	publicRefundService.listPublicRefunds(map);


		if(is==8||is==9||is==18||is==19) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getConfirmState()==null){
					return Result.fail("包含未确认的数据导出失败");
				}
				if (list.get(i).getConfirmState()!=1){
					return Result.fail("包含未确认的数据导出失败");
				}
				if (list.get(i).getExport() == 1 && ExportStateDefaultValue(list.get(i).getPublicExportState()) == 1 && ExportStateDefaultValue(list.get(i).getPrivateExportState()) == 1){
					return Result.fail("该批次已导出");
				}else if (ExportStateDefaultValue(list.get(i).getPublicExportState())== 1){
					return Result.fail("该批次对公部分已导出");
				}else if (ExportStateDefaultValue(list.get(i).getPrivateExportState()) == 1){
					return Result.fail("该批次对私部分已导出");
				}else if (list.get(i).getExport() == 1 && ExportStateDefaultValue(list.get(i).getPublicExportState()) == 0 && ExportStateDefaultValue(list.get(i).getPrivateExportState()) == 0){
					return Result.fail("该批次已导出");
				}
			}
		}

		//如果批次id不同不能导出
		Integer batchId =0;
		if (null!=list){
			batchId = list.get(0).getBatchId();
		}
		for (PublicRefundDO publicRefundDO:list){
			if (!batchId.equals(publicRefundDO.getBatchId())){
				return Result.fail("导出时必须选择同一批次！");
			}
		}
		return  Result.ok();
	}






	//对公对私导出默认值
	private Integer ExportStateDefaultValue(Integer value){
		if (null ==value){
			return 0;
		}
		return value;
	}





	/**
	 * 查询对应的考试费信息
	 *
	 */
	@RequestMapping("/selectCost")
	@RequiresPermissions("jzweb:publicRefund:import")
	@ResponseBody
	public Result<?> selectCost(@RequestParam(required = false,defaultValue = "1")int pageNumber, @RequestParam(required = false,defaultValue = "10")int pageSize, String name,Integer year) {
		// 查询列表数据
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
		//查询所有用户所属权限
		Integer is = userRoleService.userRolenum(this.getUserId());
		Map map=new HashMap();
		Long chOO1=0l;
		Long chOO2=0l;
		List<RoleDO>  listRole=roleService.findAll();
		List<String> lists=new ArrayList();
		String userID=this.getUserId().toString();
		if(is==8||is==9||is==18||is==19){
			lists.add(userID);
		}if(is==6){
			List lists1=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
					chOO1=roleDO.getId();
					lists1.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists1);
			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
			for(UserRoleDO users:userRoleDOS){
				lists.add(users.getUserId().toString());
			}
			map.put("list1",lists);
		}else if(is==7){
			List lists1=new ArrayList();

			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
					chOO1=roleDO.getId();
					lists1.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			for(UserRoleDO users:userRoleDOS){
				lists.add(users.getUserId().toString());
			}
			map.put("list1",lists);
		}

		Page<Map<Object,Object>> list1=examinationCostService.selectSchool(pageNumber,pageSize,name,lists,year);

		return Result.ok(list1);

	}





	/**
	 * 导出错误信息
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author kl
	 */
	@Log("导出错误信息")
	@GetMapping("/importError")
	@ResponseBody
	@Transactional
	@RequiresPermissions("jzweb:publicRefund:import")
	public void importError(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 告诉浏览器用什么软件可以打开此文件
		response.setHeader("content-Type", "application/vnd.ms-excel");
		// 下载文件的默认名称
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("考试返费导入错误信息", "UTF-8") + ".xls");
		//编码
		response.setCharacterEncoding("UTF-8");
		String jsonArray = EhCacheUtil.get("publicRefund" + this.getUserId()).toString();
		JSONArray arrayList = JSONArray.parseArray(jsonArray);
		//转换为目标对象list
		List<ErroeDownVo> groupList = JSONObject.parseArray(arrayList.toJSONString(), ErroeDownVo.class);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ErroeDownVo.class, groupList);
		workbook.write(response.getOutputStream());
	}




	@GetMapping("/add")
	@RequiresPermissions("jzweb:publicRefund:add")
	String add(Integer year,Model model){
		model.addAttribute("year",year);
	    return "jzweb/publicRefund/add";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:publicRefund:edit")
	String edit(@PathVariable("id") Long id,Model model){
		PublicRefundDO publicRefund = publicRefundService.selectById(id);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		publicRefund.setEvaluationTimes(sdf.format(publicRefund.getEvaluationTime()));

		model.addAttribute("publicRefund", publicRefund);
	    return  "jzweb/publicRefund/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:publicRefund:add")
	public Result<String> save( PublicRefundDO publicRefund){

		if (StringUtils.isNullOrEmpty(publicRefund.getExpertName())) {
			return Result.fail("专家姓名未填写，请完善！");
		}else if (StringUtils.isNullOrEmpty(publicRefund.getExpertIdCard())) {
			return Result.fail("专家身份证号码未填写，请完善！");
		} else if (StringUtils.isNullOrEmpty(publicRefund.getExpertType())) {
			return Result.fail("专家类型未填写，请完善！");
		} else if (null == publicRefund.getBankAccount()||"".equals(publicRefund.getBankAccount())) {
			return Result.fail("银行账户未填写，请完善！");
		} else if (null == publicRefund.getBankOfDeposit()||"".equals(publicRefund.getBankOfDeposit())) {
			return Result.fail("开户银行未填写，请完善！");
		} else if (null == publicRefund.getExamFeeIf()||"".equals(publicRefund.getExamFeeIf())) {
			return Result.fail("学校未选择，请完善！");
		}

		if (publicRefund.getBatchId() != null){
			if (publicRefund.getBatchId()<100000){
				return Result.fail("批次id不能小于100000");
			}
		}

		//如果批次id为空则进行获取最大批次id
		if (null == publicRefund.getBatchId()){
			int batchId = publicRefundService.batchMax();
			publicRefund.setBatchId(batchId);
		}

		publicRefund.setConfirmState(0);
		publicRefund.setReturnState(0);

		publicRefund.setRefundType("对私".equals(publicRefund.getRefundType())?"1":"0");

		//查询考试费信息
		ExaminationCostDO examinationCostDO=examinationCostService.selectById(publicRefund.getExamFeeIf());

		if(examinationCostDO!=null){
			publicRefund.setEvaluationSchool(examinationCostDO.getOrgName()!=null?examinationCostDO.getOrgName():null);
			publicRefund.setEvaluationTime(examinationCostDO.getExaminationDate());
			publicRefund.setEvaluationNum(examinationCostDO.getStuNum());
		}
		publicRefund.setCreateTime(new Date());
		publicRefund.setRefundRrNot(0);
		publicRefundService.insert(publicRefund);
        return Result.ok();
	}



	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:publicRefund:edit")
	public Result<String>  update(PublicRefundDO publicRefundDO){
		PublicRefundDO pubDO=publicRefundService.selectById(publicRefundDO.getId());
		//获取当前用户权限
		Integer is = userRoleService.userRolenum(this.getUserId());

		if(pubDO.getRefundRrNot()==1){
			return Result.fail("已经返费的数据不允许操作！");
		}
		//已确认
		if (pubDO.getConfirmState()==1){
			//不是市场总监
			if (is!=12){
				return Result.fail("当前角色无权限修改已经确认的数据！");
			}
		}


		if(!StringUtils.isNullOrEmpty(publicRefundDO.getEvaluationTimes())){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd ");
			try {
				publicRefundDO.setEvaluationTime(sdf.parse(publicRefundDO.getEvaluationTimes()+" 00:00:00"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		boolean update = publicRefundService.updateById(publicRefundDO);
		return update ? Result.ok() : Result.fail();
	}





	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:remove")
	public Result<String>  remove( Long id){
		PublicRefundDO publicRefundDO=publicRefundService.selectById(id);
		//获取当前用户权限
		Integer is = userRoleService.userRolenum(this.getUserId());
		if (publicRefundDO.getConfirmState()==1){
			//不是市场总监
			if (is!=12){
				return Result.fail("当前角色无权限删除已经确认的数据！");
			}
		}
		if(publicRefundDO.getRefundRrNot()==1){
			return Result.fail("已经返费的数据不允许操作！");
		}

		publicRefundService.deleteById(id);
		logService.saveOperationLog(new LogBO().setOperation("2022返费信息管理/返费删除").setUserId(String.valueOf(this.getUserId())));
        return Result.ok();
	}



	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){

		List<PublicRefundDO> publicRefundDOS = publicRefundService.selectBatchIds(Arrays.asList(ids));

		for (int i=0;i<publicRefundDOS.size();i++){
			if (publicRefundDOS.get(i).getConfirmState() == 1){ //已确认
				return Result.fail("包含已确认的数据，无法批量删除！");
			}
		}
		publicRefundService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}



	@Log("返费确认")
	@PostMapping("/batchConfirm")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:batchConfirm")
	public Result<String>  confirmCost(@RequestParam("id") Long confirmId){
		//目前是单个确认，考虑到以后可能是批量
		Long[] ids =new Long[10];
		ids[0]=confirmId;
		List<PublicRefundDO> refundDOList = publicRefundService.selectBatchIds(Arrays.asList(ids));
		for (PublicRefundDO publicRefundDO:refundDOList){
			if (1==publicRefundDO.getConfirmState()){
				return Result.fail("请勿重复确认！");
			}
			if (1==publicRefundDO.getRefundRrNot()){
				return Result.fail("已返费数据不能确认！");
			}
		}

		if (ids.length==0){
			return Result.build(500,"id不能为空");
		}
		List<PublicRefundDO> publicRefundDOS = new ArrayList<>();
		Arrays.stream(ids).forEach(id->{
			PublicRefundDO publicRefundDO =new PublicRefundDO();
			publicRefundDO.setId(id).setConfirmState(1).setConfirmDate(new Date()).setRefundRrNot(0);
			publicRefundDOS.add(publicRefundDO);
		});
		boolean ret = publicRefundService.updateBatchById(publicRefundDOS);
		if (!ret){
			return Result.fail();
		}
		return Result.ok();
	}




	@Log("返费批量确认")
	@PostMapping("/batchConfirm2")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:batchConfirm2")
	public Result<String>  confirmCost2(@RequestParam("ids[]") Long[] ids){
		List<PublicRefundDO> refundDOList = publicRefundService.selectBatchIds(Arrays.asList(ids));
		for (PublicRefundDO publicRefundDO:refundDOList){
			if (1==publicRefundDO.getConfirmState()){
				return Result.fail("请勿重复确认！");
			}
			if (1==publicRefundDO.getRefundRrNot()){
				return Result.fail("已返费数据不能确认！");
			}
		}

		if (ids.length==0){
			return Result.build(500,"id不能为空");
		}
		List<PublicRefundDO> publicRefundDOS = new ArrayList<>();
		Arrays.stream(ids).forEach(id->{
			PublicRefundDO publicRefundDO =new PublicRefundDO();
			publicRefundDO.setId(id).setConfirmState(1).setConfirmDate(new Date()).setRefundRrNot(0);
			publicRefundDOS.add(publicRefundDO);
		});
		boolean ret = publicRefundService.updateBatchById(publicRefundDOS);
		if (!ret){
			return Result.fail();
		}
		return Result.ok();
	}





	/**
	 * 查询对应的考试费信息  返费的下拉框
	 *
	 */
	@RequestMapping("/selectCost2")
	@RequiresPermissions("jzweb:refund:import")
	@ResponseBody
	public Result<?> selectCost2(@RequestParam(required = false,defaultValue = "1")int pageNumber, @RequestParam(required = false,defaultValue = "10")int pageSize, String name,Integer year) {
		// 查询列表数据
		//1 订单发布角色  2 订单审核专员   3 仓库审核专员  4超级用户角色  roleName
		//查询所有用户所属权限
		Integer is = userRoleService.userRolenum(this.getUserId());
		Map map=new HashMap();
		Long chOO1=0l;
		Long chOO2=0l;
		List<RoleDO>  listRole=roleService.findAll();
		List<String> lists=new ArrayList();
		String userID=this.getUserId().toString();
		if(is==8||is==9||is==18||is==19){
			lists.add(userID);
		}if(is==6){
			List lists1=new ArrayList();
			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场一部")||roleDO.getRoleName().equals("市场一部管理员")){
					chOO1=roleDO.getId();
					lists1.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists1);
			//Page<UserRoleDO> page1 =	userRoleService.selectPage(getPages(UserRoleDO.class), wrapper1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			//List<UserRoleDO> userRoleDOS=	page1.getRecords();
			for(UserRoleDO users:userRoleDOS){
				lists.add(users.getUserId().toString());
			}
			map.put("list1",lists);
		}else if(is==7){
			List lists1=new ArrayList();

			for(RoleDO roleDO:listRole){
				if(roleDO.getRoleName().equals("市场二部")||roleDO.getRoleName().equals("市场二部管理员")){
					chOO1=roleDO.getId();
					lists1.add(chOO1);
					//break;
				}
			}
			EntityWrapper<UserRoleDO> wrapper1 = new EntityWrapper<>();

			wrapper1.in("roleId",lists1);
			List <UserRoleDO> userRoleDOS =userRoleService.selectList(wrapper1);
			for(UserRoleDO users:userRoleDOS){
				lists.add(users.getUserId().toString());
			}
			map.put("list1",lists);
		}

		Page<Map<Object,Object>> list1=examinationCostService.selectSchool2(pageNumber,pageSize,name,lists,year);

		return Result.ok(list1);

	}



	@Log("返费退回")
	@PostMapping("/returnCost")
	@ResponseBody
	@RequiresPermissions("jzweb:publicRefund:returnCost")
	public Result<String>  rejectCost(@RequestParam("id") String id,@RequestParam("returnExplain") String returnExplain){

		PublicRefundDO prd = publicRefundService.selectById(id);

		if (prd.getConfirmState()==0){
			return Result.fail("请先确认后再进行退回操作！");
		}

		if (1==prd.getRefundRrNot()){
			return Result.fail("已返费数据无法退回！");
		}
		if (2==prd.getRefundRrNot()){
			return Result.fail("请勿重复退回！");
		}


		if (StringUtil.isNull(id)){
			return Result.build(500,"id不能为空");
		}
		PublicRefundDO publicRefundDO =new PublicRefundDO();
		publicRefundDO.setId(Long.valueOf(id)).setRefundRrNot(2).setReturnDate(new Date()).setReturnState(1).setReturnExplain(returnExplain).setConfirmState(0).setExport(0);
		boolean ret = publicRefundService.updateById(publicRefundDO);
		if (!ret){
			return Result.fail();
		}

		logService.saveOperationLog(new LogBO().setOperation("2022返费信息管理/返费退回").setUserId(String.valueOf(this.getUserId())));

		return Result.ok();
	}




	@Log("批量返费")
	@PostMapping( "/refunds")
	@ResponseBody
	@Transactional
	@RequiresPermissions("jzweb:publicRefund:refunds")
	public Result<String>  refunds(@RequestParam("ids[]") Long[] ids){
		List<PublicRefundDO> publicRefundDOS = publicRefundService.selectBatchIds(Arrays.asList(ids));
		for (PublicRefundDO publicRefundDO:publicRefundDOS){
			if (publicRefundDO.getConfirmState()==0){
				return Result.fail("未确认数据不能进行该操作！");
			}
			if (1==publicRefundDO.getRefundRrNot()){
				return Result.fail("已返费数据不能进行该操作！");
			}

		}

		for(int i=0;i<publicRefundDOS.size();i++){
			publicRefundDOS.get(i).setRefundRrNot(1);
			publicRefundDOS.get(i).setRefundTime(new Date());
			//只要返费的数据就不能再导出了所以这里默认导出状态改为1已导出
			publicRefundDOS.get(i).setExport(1);
		}

		publicRefundService.updateBatchById(publicRefundDOS);
		return Result.ok();
	}


}
